package Test;

public class 岛屿数量 {
    public static int numIslands(char[][] grid){
        int  m=grid.length;
        int n=grid[0].length;
        int res=0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]=='1') res++;
                dfs(i,j,grid);
            }
        }
        return res;
    }
    static void dfs(int i,int j,char[][] grid ){
        if(i<0||j<0||i==grid.length||j==grid[0].length) return;
        if(grid[i][j]=='0') return;
        if(grid[i][j]=='1') grid[i][j]='0';
        dfs(i+1,j,grid);
        dfs(i,j+1,grid);
        dfs(i-1,j,grid);
        dfs(i,j-1,grid);
    }

    public static void main(String[] args) {
        char[][] grid=
                {{'1','1','1','1','0'},
                 {'0','0','1','1','0'},
                 {'1','0','0','1','0'}};
        System.out.println("numIslands(grid) = " + numIslands(grid));
    }
}
